package com.lzq.model.combination;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/24 9:24
 * @updateUser : modify
 * @updateTime : 2022/11/24 9:24
 * @updateRemark : 抽象根节点
 */
public abstract class MenuComponent {
    protected String name;
    protected int level;

    public String getName() {
        return name;
    }

    public void add(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }

    public void remove(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }

    public MenuComponent getChild(int i) {
        throw new UnsupportedOperationException();
    }

    public void print() {
        throw new UnsupportedOperationException();
    }
}

class Menu extends MenuComponent {
    private List<MenuComponent> list = new ArrayList<>();

    public Menu(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void add(MenuComponent menuComponent) {
        list.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        list.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int i) {
        return list.get(i);
    }

    @Override
    public void print() {
        System.out.println("<<--------------------------------------------------------------------->>");
        System.out.println(name);
        System.out.println("<<<<< ========= >>>>>");
        for (MenuComponent item : list) {
            item.print();
        }
    }
}

class MenuItem extends MenuComponent {
    public MenuItem(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void print() {
        System.out.println(name);
    }
}

class Test {
    public static void main(String[] args) {
        MenuComponent menu = new Menu("菜单1", 2);
        menu.add(new MenuItem("节点1", 3));
        menu.add(new MenuItem("节点3", 3));
        menu.add(new MenuItem("节点2", 3));

        MenuComponent menu1 = new Menu("菜单2", 2);
        menu1.add(new MenuItem("节点4", 3));
        menu1.add(new MenuItem("节点5", 3));
        menu1.add(new MenuItem("节点6", 3));

        MenuComponent menu2 = new Menu("菜单3", 2);
        menu2.add(new MenuItem("节点7", 3));
        menu2.add(new MenuItem("节点8", 3));
        menu2.add(new MenuItem("节点9", 3));

        MenuComponent menuComponent = new Menu("菜单树", 1);
        menuComponent.add(menu);
        menuComponent.add(menu1);
        menuComponent.add(menu2);
        menuComponent.print();
    }
}